<?php

/**
 * korpa actions.
 *
 * @package    marketing
 * @subpackage korpa
 * @author     Your name here
 */
class korpaActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
     $this->korpa = array();
      if($this->getUser()->hasAttribute('korpa')){
        $this->korpa =$this->getUser()->getAttribute('korpa');
        
    }
    $this->form=new NarudzbinaForm();
  }
  public function executeUkloni(sfWebRequest $request)
  {
        
        $korpa =$this->getUser()->getAttribute('korpa');
        
        unset($korpa[$request->getParameter('id')]);
        
        $this->getUser()->setAttribute('korpa', $korpa);
        $this->setTemplate(false);
        $this->redirect("korpa/index");

    
  }

  public function executeNaruci(sfWebRequest $request)
  {

       if(!$request->hasParameter('narudzbina')) $this->redirect404();
       $params=$request->getParameter('narudzbina'); 
       $narudzbina=new Narudzbina();
       $narudzbina->setNacinIsporukeId($params['nacin_isporuke_id']);
       $narudzbina->setNacinPlacanjaId($params['nacin_placanja_id']);
       $narudzbina->setDatumNarudzbine(date('Y-m-d H:i:s', time()));
       $narudzbina->setStatusId(3);
       $narudzbina->setKupacId($this->getUser()->getId());
       $narudzbina->save();
       $narudzbinaId=$narudzbina->getId();
       $korpa=$this->getUser()->getAttribute('korpa');
       foreach($korpa as $id =>$kolicina)
           {
                $rez = new NarudzbinaProzvod();
                $rez->setNarudzbinaId($narudzbinaId);
                $rez->setProizvodId($id);
                $rez->setKolicina($kolicina);
                $rez->save();

           }

	   $this->getUser()->getAttributeHolder()->remove('korpa');
           $this->setTemplate($false);
           $this->redirect('proizvod/index');
  }


 
}
